package com.boge.flow;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class FlowableTest4Listener {

    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /**
     * 流程部署操作
     */
    @Test
    void deployFlow() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/01-base/Example02.bpmn20.xml")
                .name("任务分配案例-监听器")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());//1a446f73-85b6-11f0-adcd-56293805ff89
    }

    @Test
    void startFlow() {
        //流程定义表中动态维护的 act_re_procdef=>id
        String processDefinitionId = "Example02:1:1aa469c6-85b6-11f0-adcd-56293805ff89";
        //我们创建流程图的时候自定义的。注意保证唯一  act_re_procdef=>key
        String processKey = "Example02";
        //在流程启动的时候绑定变量
        Map<String, Object> variables = new HashMap<>();
        variables.put("assignee2","zhangsan");
        //根据流程定义id启动流程(推荐)
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId,variables);
        //根据流程定义key启动流程
        //runtimeService.startProcessInstanceByKey(processKey);
    }

    /**
     * 任务审批
     */
    @Test
    void completeTask() {
//        taskService.complete("3b0702f7-85b7-11f0-95d4-56293805ff89");//审批用户1 zhangsan
        taskService.complete("c9392534-85b7-11f0-91be-22d1c4776ba0");//审批用户2 boge
    }

}
